/*
 * Copyright 2007 Mojo Chen
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

var linkxdChLink = {
	
	onLoad: function() {
		//
		//var vbox1 = document.getElementById("chbox1");
		//var vbox2 = document.getElementById("chbox2");
		//vbox1.appendChild(this.getVboxDiv1());
		//vbox2.appendChild(this.getVboxDiv2());

		var td1 = document.getElementById("td1");
		var td2 = document.getElementById("td2");
		td1.appendChild(this.getVboxDiv1());
		td2.appendChild(this.getVboxDiv2());	
	},
	
	getVboxDiv1: function(){
		var newdiv = document.createElementNS("http://www.w3.org/1999/xhtml","html:div");
		newdiv.setAttribute("style","margin:20px;width:300px;");
		
		for(var index=0; index<10; index++) {
			var toLink = this.getRamGoodLink();
			//var toLink = this.getRamHotLink();
			var newp = document.createElementNS("http://www.w3.org/1999/xhtml","html:p");
			//var txtnode = document.createTextNode("Hot:");
			//newp.appendChild(this.getNewTagAImg(linkxdMain.xdlink,linkxdMain.xdtitle,linkxdMain.xdpngChrome));
			//newp.appendChild(txtnode);
			newp.appendChild(this.getNewTagA(toLink.url,toLink.title));
			// <image id="chImage1"/>
			//newp.appendChild(this.getImageTag("chImage2"));
			//newp.appendChild(this.getImageTag("chImage1"));
			newp.appendChild(this.getHtmlImageTag(linkxdMain.xdChromeExternal));
			//newp.appendChild(this.getButtonTag('Cut'));
			
			var topost = linkxdHtml.getLink(toLink.url,toLink.title);
			
			var ck = "linkxdMain.paste('" +
					topost +
					"');"
			newp.appendChild(this.getHtmlImageOnClickTag(linkxdMain.pngCutChrome,ck));
			newdiv.appendChild(newp);
		}
		
		newdiv.appendChild(this.getLinkxdPtag());
		
		return newdiv;
	},
	
	getVboxDiv2: function(){
		
		var newdiv = document.createElementNS("http://www.w3.org/1999/xhtml","html:div");
		
		newdiv.setAttribute("style","margin:20px;width:300px;");
		
		newdiv.appendChild(this.getLinkxdPtag2());
		
		for(var index=0; index<10; index++) {
			
			var toLink = this.getRamHotLink();			
			var newp = document.createElementNS("http://www.w3.org/1999/xhtml","html:p");
			//var txtnode = document.createTextNode("Hot:");
			newp.appendChild(this.getNewTagAImg(linkxdMain.xdlink,linkxdMain.xdtitle,linkxdMain.xdpngChrome));
			//newp.appendChild(txtnode);
			newp.appendChild(this.getNewTagA(toLink.url,toLink.title));
			// <image id="chImage1"/>
			//newp.appendChild(this.getImageTag("chImage2"));
			//newp.appendChild(this.getImageTag("chImage1"));
			newp.appendChild(this.getHtmlImageTag(linkxdMain.xdChromeExternal));
			
			var topost = linkxdHtml.getLink(toLink.url,toLink.title);
			
			var ck = "linkxdMain.paste('" +
					topost +
					"');"
					
			newp.appendChild(this.getHtmlImageOnClickTag(linkxdMain.pngCutChrome,ck));
			newdiv.appendChild(newp);
		}
		return newdiv;
	},
	
	getButtonTag: function(btnLabel){
		var button = document.createElement("button");
		button.setAttribute("label",btnLabel);
		button.setAttribute("style","width:30px;");
		return button;
	},
	
	getLinkxdPtag: function(){
		var newp = document.createElementNS("http://www.w3.org/1999/xhtml","html:p");
		var txtnode = document.createTextNode(linkxdMain.xdtitle);
		newp.appendChild(txtnode);
		newp.appendChild(this.getNewTagAImg(linkxdMain.xdlink,linkxdMain.xdtitle,linkxdMain.xdpngChrome));
		var topost = linkxdHtml.getLinkXd();
		var ck = "linkxdMain.paste('" +
					topost +
					"');"
		newp.appendChild(this.getHtmlImageOnClickTag(linkxdMain.pngCutChrome,ck));
		return newp;
	},
	
	getLinkxdPtag2: function(){
		var newp = document.createElementNS("http://www.w3.org/1999/xhtml","html:p");
		var txtnode = document.createTextNode(linkxdMain.xdtitleEn);
		newp.appendChild(txtnode);
		newp.appendChild(this.getNewTagAImg(linkxdMain.xdlink,linkxdMain.xdtitleEn,linkxdMain.xdpngChrome));
		//
		var topost = linkxdHtml.getLinkXdEn();
		var ck = "linkxdMain.paste('" +
					topost +
					"');"
		newp.appendChild(this.getHtmlImageOnClickTag(linkxdMain.pngCutChrome,ck));
		return newp;
	},
	
	getImageTag : function(idName){
		//
		var img = document.createElement("image");
		img.setAttribute("id",idName);
		return img;
	},
	
	getHtmlImageTag : function(url){
		var img = document.createElementNS("http://www.w3.org/1999/xhtml","html:img");
		img.setAttribute("src",url);
		//style="border:0;vertical-align:middle;" 
		img.setAttribute("style","border:0;vertical-align:middle;");
		return img;
	},
	
	getHtmlImageOnClickTag : function(url,onClick){
		var img = document.createElementNS("http://www.w3.org/1999/xhtml","html:img");
		img.setAttribute("src",url);
		//style="border:0;vertical-align:middle;" 
		img.setAttribute("style","border:0;vertical-align:middle;");
		img.setAttribute("onclick",onClick);
		return img;
	},
	
	getNewTagA:function(url,title){
		var newa = document.createElementNS("http://www.w3.org/1999/xhtml","html:a");
		newa.setAttribute("href",url);
		newa.setAttribute("target","_blank");
		var txtnode = document.createTextNode(title);
		newa.appendChild(txtnode);
		return newa;
	},
	
	getNewOnClickTagA:function(onClick,title){
		var newa = document.createElementNS("http://www.w3.org/1999/xhtml","html:a");
		newa.setAttribute("href","#");
		newa.setAttribute("onclick",onClick);
		var txtnode = document.createTextNode(title);
		newa.appendChild(txtnode);
		return newa;
	},
	
	getNewTagAImg:function(url,title,imgUrl){
		var newa = document.createElementNS("http://www.w3.org/1999/xhtml","html:a");
		newa.setAttribute("href",url);
		newa.setAttribute("target","_blank");
		var img = this.getHtmlImageTag(imgUrl);
		newa.appendChild(img);
		return newa;
	},
	
  	getRamHotLink:function(){
  		var result = {};
  		var hotLen = linkxdDbTagHot.keys.length;
  		var ranNum= Math.floor(Math.random()*hotLen);
  		var hot = linkxdDbTagHot.keys[ranNum];
  		result.title = hot+'('+linkxdDbTagHot.title+')';
  		result.url = linkxdDbTagHot.url+hot;
  		return result;
  	},
  	
  	getRamGoodLink:function(){
  		var result = {};
  		var len = linkxdTagGood.links.length;
  		var ranNum= Math.floor(Math.random()*len);
  		var hot = linkxdTagGood.links[ranNum];
  		result.title = hot.title;
  		result.url = hot.url;
  		return result;
  	}
  
};
window.addEventListener("load", function(e) { linkxdChLink.onLoad(e); }, false);
